package com.vcredit.bird.monitor;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class SendMailMessage implements SendMonitorMessage{
	
	@Autowired
	private JavaMailSenderImpl mailSender;
	
	@Value("${mail.send.person}")
	private String MAIL_SEND_PERSON;
	
	@Value("${mail.copy.person}")
	private String MAIL_COPY_PERSON;
	
	@Value("${mail.username}")
	private String MAIL_USERNAME;
	
	public void sendMail(String subject,String content) throws Exception{
		MimeMessage  mailMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
		helper.setFrom(new InternetAddress(MAIL_USERNAME));
		helper.setSubject(subject);
		helper.setText(content);
		helper.setTo(InternetAddress.parse(MAIL_SEND_PERSON));
		helper.setCc(InternetAddress.parse(MAIL_COPY_PERSON));
		mailSender.send(mailMessage);
	}

	@Override
	public void sendMessage(Message message) throws Exception {
		if(null!=message){
			sendMail(message.getSubject(), message.getContent());
		}
	}
}
